const Mock = require('mockjs');
const jsonServer = require('json-server');
const server = jsonServer.create()

const {list} = Mock.mock({
	"list|100": [
		{
			"id|+1": 1,
			"image": "@image(100x100,@color)",
			"title": "@ctitle(3,10)",
			"type|1-5": 1,
			"hot|1-100": 1,
			"time": "@dateTime"
		}
	]
})

console.log(list)

server.get('/api/list',(req,res) => {
	const {pageSize, page, sort, type} = req.query;
	let initList = [...list];
	if(type) {
		initList = initList.filter(item => item.type == type)
	}
	if(Number(sort)) {
		if(sort == 1) {
			initList = initList.sort((x,y) => new Date(y.time).getTime() - new Date(x.time).getTime())
		} else if(sort == 2) {
			initList = initList.sort((x,y) => y.hot - x.hot)
		}
	}
	res.send({
		code: 200,
		total: initList.length,
		data: initList.slice((page-1)*pageSize, page*pageSize)
	})
})


server.listen(3000,() => {
	
})